<?php

namespace App\Api\V1\Controllers;

use App\Models\Group;
use App\Models\GroupPost;
use Carbon\Carbon;

class GroupController extends BaseController
{

    public function index()
    {
        $post_list = GroupPost::orderBy('id', 'desc')->paginate(10);

        foreach ($post_list as $k => $v) {
            $post_list[$k]->content = str_limit(strip_tags($v->content), 255);
            $post_list[$k]->user = $v->user()->first(['name', 'avatar']);
            $post_list[$k]->group = $v->group()->first(['title', 'id']);
            $post_list[$k]->images = GroupPost::disposeImages($v->images);
            $post_list[$k]->time = Carbon::parse($v->created_at)->diffForHumans();
        }

        $data['list'] = $post_list;

        $group_list = Group::get(['id','title','detail','logo']);
        foreach ($group_list as $k=>$v){

            $group_list[$k]['title'] = str_limit($v->title,12,'');
            $group_list[$k]['detail'] = str_limit($v->detail,15);

            $group_list[$k]->logo = Group::setLogoUrl($v->logo);
        }

        $data['groups'] = $group_list;

        return $this->response->array(['status_code' => 200, 'message' => 'success', 'data' => $data]);
    }

}